<template>
  <el-dialog 
    v-model="commentsDialog" 
    @close="handleCancel"
    title="用户评论详情"
    width="70%"
  >
    <div class="comment-container">
      <el-card 
        v-for="(comment, index) in comments"
        :key="index"
        class="comment-item"
        shadow="hover"
      >
        <div class="book-info">
          <!-- 书籍封面 -->
          <el-image 
            :src="comment.book_url"
            fit="cover"
            class="book-cover"
            :lazy="true"
          >
            <template #error>
              <div class="image-error">封面加载失败</div>
            </template>
            <template #placeholder>
              <div class="image-loading">加载中...</div>
            </template>
          </el-image>

          <!-- 书籍基本信息 -->
          <div class="book-meta">
            <div class="book-title">
              《{{ comment.book_name }}》
              <el-tag 
                :type="getCommentTagType(comment.user_comments)"
                size="small"
                class="comment-tag"
              >
                {{ comment.user_comments }}
              </el-tag>
            </div>
            <div class="book-author">作者：{{ comment.book_author }}</div>
          </div>
        </div>

        <!-- 评论内容 -->
        <div class="comment-content">
          <div class="comment-label">评论内容：</div>
          <p class="comment-text">{{ comment.comments_content }}</p>
        </div>
      </el-card>
    </div>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { watch,ref } from 'vue'
import api from "../../api/api"

const props = defineProps({
  commentsDialog: {
      type: Boolean,
      required: true
  },
  openId: {
      type: String,
      default: ''
  }
})
const getCommentTagType = (comment) => {
  switch(comment) {
    case '好看': return 'success'
    case '一般': return 'warning'
    default: return 'info'
  }
}
const emit = defineEmits(['update:commentsDialog', 'save'])
const comments = ref([])
watch(
  () => props.commentsDialog,
  async (newVal) => {
      if (newVal && props.openId!='') {
        try{
          await api.post('/getAllUserComment', { bookId: props.openId })
              .then(response => {
                  // console.log('评论数据：', response.data)
                  comments.value=response.data
                  console.log('评论数据：', comments.value)
              })
              .catch(error => {
                  console.error('请求失败:', error)
                  ElMessage.error('获取评论失败')
              })
            }catch (error) {
          ElMessage.error('获取评论失败')
        }
      }
  }
)

const handleCancel = () => {
  emit('update:commentsDialog', false)
}
</script>

<style scoped>
.comment-container {
  max-height: 70vh;
  overflow-y: auto;
  padding: 10px;
}

.comment-item {
  margin-bottom: 20px;
  transition: all 0.3s;
}

.book-info {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.book-cover {
  width: 80px;
  height: 120px;
  margin-right: 20px;
  border-radius: 4px;
}

.book-meta {
  flex: 1;
}

.book-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #333;
}

.book-author {
  font-size: 14px;
  color: #666;
}

.comment-content {
  border-top: 1px solid #eee;
  padding-top: 15px;
}

.comment-label {
  font-size: 14px;
  color: #999;
  margin-bottom: 8px;
}

.comment-text {
  font-size: 14px;
  line-height: 1.6;
  color: #444;
  margin: 0;
}

.comment-tag {
  margin-left: 10px;
  vertical-align: text-top;
}

.image-error,
.image-loading {
  width: 80px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  color: #909399;
  font-size: 12px;
}
</style>